package android.Graphics;

import javax.microedition.khronos.opengles.GL10;
import java.nio.FloatBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: akuryou
 * Date: 4/25/11
 * Time: 10:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class InitBoard
{
    private int boardsize;
    private int matrix; // this is the gametype, 3-, 4- or 5 in a row
    private int players;
    private float height;
    private float width;
    private float[] points;

    private FloatBuffer vertexBuffer;

    public InitBoard( float width, float height, int matrix, int players )
    {
        this.width = width;
        this.height = height;
        this.matrix = matrix;
        this.players = players;
        initBoard();
    }

    /**
     * Makes the buffer that is used for drawing the vertices
     */

    public void initBoard()
    {
        points = frame( width, height, matrix );
        vertexBuffer = FloatBuffer.allocate( points.length );
        vertexBuffer = FloatBuffer.wrap( points );
        vertexBuffer.put( points );
        vertexBuffer.position( 0 );
    }

    /**
     * Uses the vertexBuffer from the initBoard() method, and draws the gamefield
     * @param gl
     */

    public void drawBoard( GL10 gl )
    {
        gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertexBuffer );
        gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
        gl.glDrawArrays( GL10.GL_LINES, 0, points.length/2 );
    }


    /**
     * Calculates the vertices that will be used for drawing the board,
     * vertical lines first, then horizontal lines, 2 coordinates per vertex (x, y).
     * @param width
     * @param height
     * @param matrix
     * @return float array to be sent into the floatbuffer
     */
    public float[] frame( float width, float height, int matrix )
    {
        float[] vertical;
        float[] horizontal;

        if( matrix == 3 && players == 3 ) { matrix = 2*matrix; }
        if( matrix == 4 ) { matrix += 3; }        //7x7 matrix
        if( matrix == 5 ) { matrix += 3;}  //8x8 matrix
        if( matrix == 5 && players == 3 ) { matrix = 3 * matrix; }

        boardsize = matrix;
        int length = ((matrix-1)*2)*2; // Calculates the amount of points it takes to draw the board with x and y coordinates


        vertical = new float[length];
        horizontal = new float[length];
        points = new float[2*length];

        /**
         * Draws up the board, vertical lines first, then horizontal lines.
         */

        for( int i = 0; i < length; i += 4 )
        {
            int mult = (i/4)+1;
            vertical[i] =   mult*(width/matrix);        // x-point
            vertical[i+1] = height;                     // y-point
            vertical[i+2] = vertical[i];                // x-point
            vertical[i+3] = 0;                          // y-point
            horizontal[i] = 0;                          // x-point
            horizontal[i+1] = mult*(height/matrix);     // y-point
            horizontal[i+2] = width;                    // x-point
            horizontal[i+3] = horizontal[i+1];          // y-point
        }
        for( int j = 0; j < points.length; ++j )
        {
            if( j < length ) /* Copies over the vertical line points into the first half of the array*/
            {
                points[j] = vertical[j];
            }
            if( j >= length ) /* Copies over the horizontal line points into the second half of the array */
            {
                points[j] = horizontal[j-length];
            }
        }
        return points;
    }
}